package org.ielts.domain.base;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import org.apache.commons.beanutils.BeanUtils;
import org.ielts.domain.Atom;
import org.ielts.domain.LearnStatus;
import org.ielts.domain.log.OrderLessonLog;
import org.ielts.domain.log.SignCourseLog;
import org.ielts.util.DateUtil;

@Entity
public class SignCourse extends Atom {

	public SignCourse() {

	}

	public SignCourse(SignCourse signCourse, long usedTimes) throws IllegalAccessException, InvocationTargetException {

		this.setBeginTime(signCourse.getBeginTime());
		this.setBook(signCourse.getBook());
		this.setCardId(signCourse.getCardId());
		this.setContractId(signCourse.getContractId());
		this.setCourse(signCourse.getCourse());
		this.setEndTime(signCourse.getEndTime());
		this.setId(signCourse.getId());
		this.setLearnStatus(signCourse.getLearnStatus());
		this.setLessonCard(signCourse.getLessonCard());
		this.setName(signCourse.getName());
		this.setNote(signCourse.getNote());
		this.setOrderLessonLogs(signCourse.getOrderLessonLogs());
		this.setRealEndTime(signCourse.getRealEndTime());
		this.setSignCourseLogs(signCourse.getSignCourseLogs());
		this.setSignTime(signCourse.getSignTime());
		this.setStudent(signCourse.getStudent());
		this.setTerm(signCourse.getTerm());
		this.setTimes(signCourse.getTimes());
		this.setTitle(signCourse.getTitle());

		this.usedTimes = (int) usedTimes;
	}

	@ManyToOne(fetch = FetchType.EAGER, optional = true)
	private Student student;

	@ManyToOne(fetch = FetchType.EAGER)
	private Course course;
	private String cardId;
	private String contractId;
	private String lessonCard;
	private String term;

	private Date beginTime;
	private Date signTime;

	@Column(length = 300)
	private String note;

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "signCourse")
	private Set<OrderLessonLog> orderLessonLogs = new HashSet<OrderLessonLog>(0);

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "signCourse")
	private Set<SignCourseLog> signCourseLogs = new HashSet<SignCourseLog>(0);

	@Transient
	private int usedTimes;

	private int times;
	private Date endTime;

	private Date realEndTime;

	private String book;

	@Enumerated(EnumType.STRING)
	private LearnStatus learnStatus = LearnStatus.Learning;

	public LearnStatus getLearnStatus() {
		return learnStatus;
	}

	public void setLearnStatus(LearnStatus learnStatus) {
		this.learnStatus = learnStatus;
	}

	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}

	public String getCardId() {
		return cardId;
	}

	public void setCardId(String cardId) {
		this.cardId = cardId;
	}

	public int getTimes() {
		return times;
	}

	public void setTimes(int times) {
		this.times = times;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public String getContractId() {
		return contractId;
	}

	public void setContractId(String contractId) {
		this.contractId = contractId;
	}

	public String getTerm() {
		return term;
	}

	public void setTerm(String term) {
		this.term = term;
	}

	public Date getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(Date beginTime) {
		this.beginTime = beginTime;
	}

	public String getLessonCard() {
		return lessonCard;
	}

	public void setLessonCard(String lessonCard) {
		this.lessonCard = lessonCard;
	}

	public String getBook() {
		return book;
	}

	public void setBook(String book) {
		this.book = book;
	}

	public Date getRealEndTime() {
		return realEndTime;
	}

	public void setRealEndTime(Date realEndTime) {
		this.realEndTime = realEndTime;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	@Transient
	public int getUsedTimes() {
		return usedTimes;
	}

	public void setUsedTimes(int usedTimes) {
		this.usedTimes = usedTimes;
	}

	public Set<OrderLessonLog> getOrderLessonLogs() {
		return orderLessonLogs;
	}

	public void setOrderLessonLogs(Set<OrderLessonLog> orderLessonLogs) {
		this.orderLessonLogs = orderLessonLogs;
	}

	public long getRemainDay() {
		if (endTime == null) {
			return 0;
		}
		long result = DateUtil.getGapDay(DateUtil.now(), endTime);
		if (result < 0) {
			result = 0;
		}
		return result;
	}

	public String getRemainDayRate() {
		if (beginTime == null || endTime == null) {
			return "";
		}
		return DateUtil.getGapDayRate(beginTime, endTime);
	}

	public Set<SignCourseLog> getSignCourseLogs() {
		return signCourseLogs;
	}

	public void setSignCourseLogs(Set<SignCourseLog> signCourseLogs) {
		this.signCourseLogs = signCourseLogs;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public Date getSignTime() {
		return signTime;
	}

	public void setSignTime(Date signTime) {
		this.signTime = signTime;
	}
}
